//
//  HCAskViewController.m
//  HappyChildren
//
//  Created by WillyZhao on 2017/1/18.
//  Copyright © 2017年 WillyZhao. All rights reserved.
//

#import "HCAskViewController.h"

@interface HCAskViewController () <UITextViewDelegate> {
    UILabel *placeholderLabel;
    UITextView *textV;
}

@end

@implementation HCAskViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.textTitle = @"提问";
    
    
    textV = [[UITextView alloc] initWithFrame:CGRectMake(YBKAdaptiveW(23), YBKAdaptiveH(39), (YBKWinFrame.size.width - YBKAdaptiveW(23)*2.0), YBKAdaptiveH(170))];
    textV.backgroundColor = YBKRGBCOLOR(215, 215, 215, 1);
    textV.layer.cornerRadius = 5.0f;
    textV.delegate = self;
    [self.centerView addSubview:textV];
    
    placeholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, textV.frame.size.width, 20)];
    placeholderLabel.textColor = [UIColor lightGrayColor];
    placeholderLabel.text = @"请描述你的问题...";
    placeholderLabel.font = [UIFont systemFontOfSize:13];
    [textV addSubview:placeholderLabel];
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(YBKAdaptiveW(50), textV.frame.origin.y+textV.frame.size.height+YBKAdaptiveH(15), (YBKWinFrame.size.width - YBKAdaptiveW(50)*2.0), YBKAdaptiveH(30))];
    btn.backgroundColor = YBKRGBCOLOR(248, 103, 97, 1);
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setTitle:@"提交" forState:UIControlStateNormal];
    btn.layer.cornerRadius = 5.0f;
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    [btn addTarget:self action:@selector(postBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.centerView addSubview:btn];
    
    self.dataRequest.delegate = self;
    
    // Do any additional setup after loading the view from its nib.
}

-(void)postBtnAction:(UIButton *)btn {
    NSLog(@"提交");
    if ([PublicFunction isNull:textV.text]) {
        [XHToast showBottomWithText:@"问题不能为空哦"];
    }else{
        [self.dataRequest postHC_MineAskToNet:HCGETUSER(@"userid") questionText:textV.text];
    }
}

-(void)dataRequesterFinish:(DataRequester *)requester requesterWithType:(DataRequestType)aType receiveDictionary:(NSDictionary *)dictionary {
    NSLog(@"提交问题:%@",dictionary);
    if ([dictionary[@"stat"] integerValue] == 200) {
        [self.navigationController popViewControllerAnimated:YES];
        [XHToast showBottomWithText:@"提问成功,请耐心等待回答哦。。。"];
    }else{
        [XHToast showBottomWithText:@"提问失败"];
    }
}

-(void)dataRequesterFail:(DataRequester *)requester requesterWithType:(DataRequestType)aType receiveErrorDictionary:(NSDictionary *)dictionary {
    NSLog(@"提交问题失败");
    [XHToast showBottomWithText:@"提交问题失败"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

{    if (![text isEqualToString:@""])
    
{
    
    placeholderLabel.hidden = YES;
    
}
    
    if ([text isEqualToString:@""] && range.location == 0 && range.length == 1)
        
    {
        
        placeholderLabel.hidden = NO;
        
    }
    
    return YES;
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
